package com.tanhua.dubbo.api;


import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class UserLikeApiImpl implements UserLikeApi{

    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 保存更新
     * @param userId
     * @param likeUserId
     * @param isLike
     * @return
     */
    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {
        try{
            //查询数据
            Query query=Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //如果不存在，保存
            if(userLike == null){
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            }else{
                //如果存在，更新
                Update update=Update.update("isLike",isLike)
                        .set("created",System.currentTimeMillis());
                mongoTemplate.updateFirst(query,update,UserLike.class);

            }
            return true;
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }

    //统计互相喜欢/喜欢/粉丝个数
    @Override
    public Map counts(Long userId) {
        List<UserLike> eachLike = findEachLike(userId);
        List<UserLike> userLikes = getUserLikes(userId);
        List<UserLike> likeMeUser = getLikeMeUser(userId);
        Map map = new HashMap();
        map.put("eachLoveCount",eachLike.size());
        map.put("loveCount", userLikes.size());
        map.put("fanCount", likeMeUser.size());
        return map;
    }


    //查询互相喜欢的用户
    @Override
    public List<UserLike> findEachLike(Long userId) {
        List<UserLike> userLikes =getUserLikes(userId);
        if(CollUtil.isEmpty(userLikes)){
            return new ArrayList<>();
        }
        List<Long> userIds = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
        Query query=Query.query(Criteria.where("userId").in(userIds).and("likeUserId").is(userId).and("isLike").is(true));
        List<UserLike> eachLikes = mongoTemplate.find(query, UserLike.class);
        return eachLikes;
    }

    //查询我喜欢的用户
    @Override
    public List<UserLike> getUserLikes(Long userId) {
        List<UserLike> userLikes=mongoTemplate.find(Query.query(Criteria.where("userId").is(userId).and("isLike").is(true))
                .with(Sort.by(Sort.Order.desc("created"))),UserLike.class);
        return userLikes;
    }

    //查询喜欢我的用户
    @Override
    public List<UserLike> getLikeMeUser(Long userId) {
        List<UserLike> userLikes=mongoTemplate.find(Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true))
                .with(Sort.by(Sort.Order.desc("created"))),UserLike.class);
        return userLikes;
    }

    /**
     * 查询当前用户是否喜欢指定用户
     *
     * @param likeUserId
     * @param userId
     * @return
     */
    @Override
    public Boolean likeOrNot(Long userId, Long likeUserId) {
        boolean exists =mongoTemplate.exists(Query.query(Criteria.where("userId").is(userId)
                .and("likeUserId").is(likeUserId)
                .and("isLike").is(true)),UserLike.class);
        return exists;
    }
}
